// Copyright 2016-2101 Pica.
package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserManagerService;
import org.apache.commons.collections.BagUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description TODO
 * @Author yuefan
 * @Date 2020-11-23 10:11
 */
@RestController
@RequestMapping("/userManager")
public class UserManagerController {

    @Reference
    private UserManagerService userManagerService;

    @RequestMapping("/findAll")
    public Result findAll(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult result = userManagerService.findAll(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MEMBERINFORM_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBERINFORM_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result addUser(Integer[] roleIds, @RequestBody User user) {

        try {
            userManagerService.add(user, roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);

        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer userId) {
        try {
            User user=userManagerService.findById(userId);
            if (user != null) {
                return new Result(true, MessageConstant.QUERY_MEMBERINFORM_SUCCESS, user);
            }
            return new Result(false, MessageConstant.QUERY_MEMBERINFORM_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBERINFORM_FAIL);
        }
    }

    @RequestMapping("/findRoles")
    public Result findRoles() {
        try {
            List<Role> list = userManagerService.findRoles();
            return new Result(true, MessageConstant.QUERY_ROLES_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLES_FAIL);
        }
    }
    @RequestMapping("/findRoleIdsByUserId")
    public List findRoleIdsByUserId(Integer userId) {
        try {
            List<Integer> list = userManagerService.findRoleIdsByUserId(userId);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] roleIds, @RequestBody User user) {
        try {
            userManagerService.edit(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

    @RequestMapping("/deleteUser")
    public Result deleteUser(Integer userId) {
        try {
            userManagerService.delete(userId);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);

        }

    }
    @RequestMapping("/findByQueryString")
    public Result queryString(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult result = userManagerService.findByQueryString(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MEMBERINFORM_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBERINFORM_FAIL);
        }

    }


}




